package com.woniuxy.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.feign.GoodsFeign;
import com.woniuxy.feign.UserFeign;
import com.woniuxy.feign.UtilFeign;
import com.woniuxy.mapper.BidderMapper;
import com.woniuxy.mapper.GoodsMapper;
import com.woniuxy.mapper.NoticeMapper;
import com.woniuxy.service.NoticeService;
import com.woniuxy.util.TimeUtil;
import com.work.framework.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: wanghao
 * @Date: 2019/9/23 9:08
 *
 * 通知信息业务逻辑处理
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Resource
    private GoodsFeign goodsFeign;
    @Resource
    private UtilFeign utilFeign;
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private BidderMapper bidderMapper;
    @Resource
    private UserFeign userFeign;
    @Resource
    private GoodsMapper goodsMapper;

    /**
     * 发送商品开始的信息
     * @param aid
     * @return
     */
    @Override
    @Transactional
    public Boolean sendStartMessage(Integer aid) {
        //1.获取商品信息
        GoodsAuctionDTO goodsAuctionDTO = goodsFeign.selectGoodsAndAuctionByAid(aid);
        if (goodsAuctionDTO == null) {
            return false;
        }
        //查询收藏的人
        List<User> users = goodsFeign.selectUsers(goodsAuctionDTO.getGid());
        if (users == null ) {
            return false;
        }

        //查找参与竞拍的人的uid
        List<Integer> biddersUid = bidderMapper.selectUidByAid(goodsAuctionDTO.getAid());
        List<User> bidderUsers = userFeign.findUsers(JSON.toJSONString(biddersUid));
        if (bidderUsers == null) {
            return false;
        }
        System.out.println("bidderUsers");
        //user去重
        Set<User> userSet = new HashSet<>(bidderUsers);
        userSet.addAll(users);
        String startTime = goodsAuctionDTO.getStart();
        String goodsName = goodsAuctionDTO.getGname();
        String mes = "尊敬的用户，你收藏的"+goodsName+"将在"+startTime+"开始拍卖";

        String localTime = TimeUtil.getLocalTime();
        Notice notice = new Notice(1,null,2,mes,1, localTime,"");
        for (User user : userSet) {
            //2.发送短信
            String isSendOk = utilFeign.sendTime(user.getAccount(), localTime, goodsName);
            if (isSendOk == null || isSendOk.contains("失败")) {
                System.out.println(user.getAccount()+"短信没有发送成功");
            }
            //3.发送通知
            notice.setRUid(user.getUid());
            int insert = noticeMapper.insert(notice);
            if (insert <= 0) {
                System.out.println(user.getAccount()+"数据没有插入成功");
            }
        }
        return true;
    }

    /**
     * 发送拍卖即将结束
     * @param aid
     * @return
     */
    @Override
    @Transactional
    public Boolean sendEndMessage(Integer aid) {
        //1.获取商品信息
        GoodsAuctionDTO goodsAuctionDTO = goodsFeign.selectGoodsAndAuctionByAid(aid);
        if (goodsAuctionDTO == null) {
            return false;
        }
        //查询收藏的人
        List<User> users = goodsFeign.selectUsers(goodsAuctionDTO.getGid());
        if (users == null ) {
            return false;
        }
        //查找参与竞拍的人的uid
        List<Integer> biddersUid = bidderMapper.selectUidByAid(goodsAuctionDTO.getAid());
        List<User> bidderUsers = userFeign.findUsers(JSON.toJSONString(biddersUid));
        if (bidderUsers == null) {
            return false;
        }
        //user去重
        Set<User> userSet = new HashSet<>(bidderUsers);
        userSet.addAll(users);

        String endTime = goodsAuctionDTO.getStop();
        String goodsName = goodsAuctionDTO.getGname();
        String mes = "尊敬的用户，你收藏的"+goodsName+"将在"+endTime+"结束拍卖";
        String localTime = TimeUtil.getLocalTime();
        Notice notice = new Notice(1,null,2,mes,1, localTime,"");

        for (User user : userSet) {

            //2.发送短信
            String isSendOk = utilFeign.sendTime(user.getAccount(),localTime , goodsName);
            if (isSendOk == null || isSendOk.contains("失败")) {
                System.out.println(user.getAccount()+"短信没有发送成功");
            }
            //3.发送通知

            notice.setRUid(user.getUid());
            int insert = noticeMapper.insert(notice);
            if (insert <= 0) {
                System.out.println(user.getAccount()+"数据库没有插入成功");
            }

        }
        return true;
    }

    /**
     * 发送成功的信息
     * @param uid
     * @return
     */
    @Override
    @Transactional
    public Boolean sendSuccessMessage(Integer uid, Integer gid) {
        Map<String, Integer> params = new HashMap<>();
        params.put("uid", uid);
        params.put("gid", gid);
        Goods goods = goodsMapper.findGoodsByUidAndGid(params);
        String localTime = TimeUtil.getLocalTime();
        if (null == goods || "".equals(goods.getGname())) {
            System.out.println("没有找到商品");
            return false;
        }
        String goodsName = goods.getGname();
        //根据uid查找user
        User user = userFeign.findUser(uid);
        if (null == user || "".equals(user.getAccount())) {
            System.out.println("没有找到用户");
            return false;
        }
        //1.发送短信
        String result = utilFeign.sendMessage(user.getAccount(), goodsName);
        if (null == result) {
            return false;
        }
        String mes = "尊敬的用户，你参与竞拍的"+goodsName+"竞拍成功"+"请及时支付尾款！";
        //初始化通知对象
        Notice notice = new Notice(1,user.getUid(),2,mes,1, localTime,"");
        //2.将通知存数据库
        int resoult = noticeMapper.insert(notice);
        if (resoult <= 0) {
            System.out.println(user.getAccount()+"数据库没有插入成功");
        }

        return true;
    }


    /**
     * 储存买家和卖家发送的信息
     * @param notice
     * @return
     */
    @Override
    public Boolean addChatNotice(Notice notice) {
        int insert = noticeMapper.insert(notice);
        if (insert <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public ResultDTO findNoticeByUid(Integer sUid, Integer rUid, Integer mType) {
        ResultDTO dto = new ResultDTO();
        if (null == rUid) {
            dto.setCode(500);
            dto.setMessage("没有查到消息");
            return dto;
        }
        Notice notice = new Notice();
        notice.setRUid(rUid);
        notice.setSUid(sUid);
        notice.setMType(mType);
        //只查询未软删除的消息
        notice.setFlage(1);
        List<Notice> list = noticeMapper.findNoticeById(notice);
        if (null == list || 0 == list.size()) {
            dto.setCode(500);
            dto.setMessage("没有查到消息");
            return dto;
        }
        dto.setCode(200);
        dto.setData(list);
        return dto;
    }


}
